#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) The PyAMF Project.
# 影片列表 影片报告
# See LICENSE.txt for details.

import kernel.base.data as user_data
import kernel.base.function as func

class movie_list:
    classname=None
    
    def __init__(self):
        self.classname="movie_list"
        
    def report_movie(self,user_id,mlist):
        """*
         * 报告影片特征码
         * @param integer user_id 用户ID
         * @param list mlist 特征码列表
         * @return arr
        """
        fp=user_data.get_user(user_id)
        ip=int(fp["ip"])
        
        for obj in mlist:
            self.report_movie_(ip, obj)
            
        return True
        
    def report_movie_(self,ip,obj):
        """*
         * 报告影片特征码(具体执行代码)
         * @param integer ip 上报放的用户IP
         * @param dict obj 上报过来的特征码对象
         * @return arr
        """
        mdata=user_data.storage().findone("movie_group",{"signature":obj["signature"]})
        if(mdata==None):
            addlist={
                     "time":func.systime(),
                     "signature":obj["signature"],
                     "movie_name":obj["name"],
                     "ip":[ip]
                     }
            user_data.storage().add("movie_group",addlist)
            return True
        
        upip=list(mdata["ip"])
        for tmpip in upip:
            if(int(tmpip)==ip):return True
            
        upip.append(ip)
        user_data.storage().update("movie_group",{"ip":upip},{"signature":obj["signature"]})
        return True
            
                
    def addlist(self,user_id,movie_name,signature,mtype1,mtype2,movie_info,movie_pic):
        """*
         * 添加影片介绍
         * @param integer user_id 用户ID
         * @param string movie_name 影片名称
         * @param string signature 影片特征码
         * @param integer mtype1 影片类型 1电影,2电视剧,3娱乐节目
         * @param integer mtype2 电影[1枪战,2古装,3戏剧,4..] 2电视剧[1枪战,2古装,3戏剧,4..] 娱乐[1综艺,2歌唱,3装扮]
         * @param string movie_info 影片介绍 影片特征码
         * @param movie movie_pic 影片宣传照
         * @return arr
        """
        mdata=user_data.storage().findone("movie_data",{"signature":signature})
        if(mdata):
            return False
        
        addlist={
                         "e":1,
                         "time":func.systime(),
                         "movie_name":movie_name,
                         "signature":signature,
                         "mtype1":mtype1,
                         "mtype2":mtype2,
                         "movie_info":movie_info,
                         "movie_pic":movie_pic,
                    }
        user_data.storage().add("movie_data",addlist)
        return True
    
    def mlist(self,user_id):
        """*
         * 影片列表(其实是影片介绍)
         * @param integer user_id 用户ID
         * @return arr
        """
        mdata=user_data.storage().findsort("movie_data",{"e":1},None,["time",1])
        listdata=[]
        for obj in mdata:
            del obj["_id"]
            listdata.append(obj)
        return listdata
        
    def query_movie(self,user_id,signature):
        """*
         * 影片查询
         * @param integer user_id 用户ID
         * @param string signature 特征码
         * @return arr
        """
        fp=user_data.get_user(user_id)
        ips=func.int2ip(fp["ip"])
        jsondata=user_data.agreement("api.movie_list", "links", {"res":True,"msg":ips})
        
        tmpwdata=[]
        udpdata=[]
        mdata=user_data.storage().findone("movie_group",{"signature":signature},{"ip":1})
        tmpdata=user_data.storage().find("movie_external",{"signature":signature},{"url":1})
        
        for obj in tmpdata:
            tmpwdata.append(obj["url"])
            
        if(mdata):
            for objip in mdata["ip"]:
                user_ipinfo=user_data.get_userip(int(objip))
                if(user_ipinfo and user_ipinfo["fp"]!=fp["fp"]):
                    func.sendsocet(user_ipinfo["fp"],jsondata)  #向对方发送消息,以此打通udp通道
                    udpdata.append(func.int2ip(int(objip)))
        
            if(len(udpdata)>0):
                jsondata=user_data.agreement("api.movie_list", "udp_down", {"res":True,"msg":udpdata})
                func.sendsocet(fp["fp"],jsondata)
                return False
            
        #上面不执行,就用外部的
        jsondata=user_data.agreement("api.movie_list", "url_down", {"res":True,"msg":tmpwdata})
        func.sendsocet(fp["fp"],jsondata)
        return False
        
    def query_name_movie(self,user_id,name):
        """*
         * 影片查询
         * @param integer user_id 用户ID
         * @param string name 影片名称
         * @return arr
        """
        fp=user_data.get_user(user_id)
        tmpwdata=[]
        res_ip=[]
        
        import re
        pattern = re.compile(".*"+name+".*")    #实现模糊查询
        query = {"movie_name":pattern}
        print(query)
#        db.friend.find({"movie_name":/^张.*/}) #可以测试下这个
#        query = {"movie_name":"/^"+name+".*/"}
        movie_lists=[]
        mdata1=user_data.storage().find("movie_data",query)
        mdata2=user_data.storage().find("movie_group",query)
        mdata3=user_data.storage().find("movie_external",query)
        
        for obj in mdata1:
            res_ip.append(obj)
            
        for obj in mdata2:
            res_ip.append(obj)

        for obj in mdata3:
            tmpwdata.append(obj)
        
        for obj in res_ip:
            user_ipinfo=user_data.get_userip(int(obj["ip"]))
            if(user_ipinfo):
                movie_lists.append({"movie_name":obj["movie_name"],"signature":obj["signature"],"type":"udp"})
        
        for obj in tmpwdata:
            for obj2 in movie_lists:
                if(obj2["signature"]!=obj["signature"]):
                    movie_lists.append({"movie_name":obj["movie_name"],"signature":obj["signature"],"type":"http"})
            
        
        jsondata=user_data.agreement("api.movie_list", "query_list", {"res":True,"msg":movie_lists})
        func.sendsocet(fp["fp"],jsondata)
        return False
        